package cl.clientesws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class PersonasSOAWS {
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;
	/*
	 *
	 */
	public void coneccionwsPropio(){
		NAMESPACE = "http://ws";
		METHOD_NAME = "add";
		URL = "http://192.168.1.2:8080/TallerSOA/services/PersonaSOA";

	}

	/**
	 *
	 * @param rut
	 * @param verificador
	 * @param nombre
	 * @param sexo
	 * @return
	 */
	public String addPersona(String rut, String verificador, String nombre,
			int sexo){
		coneccionwsPropio();
		METHOD_NAME = "add";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo addrut = new PropertyInfo();
		PropertyInfo addverificador = new PropertyInfo();
		PropertyInfo addnombre = new PropertyInfo();
		PropertyInfo addsexo = new PropertyInfo();
		addrut.setName("rut");
		addrut.setValue(rut);
		addrut.setType(String.class);
		addverificador.setName("verificador");
		addverificador.setValue(verificador);
		addverificador.setType(String.class);
		addnombre.setName("nombre");
		addnombre.setValue(nombre);
		addnombre.setType(String.class);
		addsexo.setName("sexo");
		addsexo.setValue(sexo);
		addsexo.setType(int.class);
		request.addProperty(addrut);
		request.addProperty(addverificador);
		request.addProperty(addnombre);
		request.addProperty(addsexo);
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}


	public String updatePersona(String nomRut, int campo, String nuevoRutName
			){
		coneccionwsPropio();
		METHOD_NAME = "update";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo updatenomRut = new PropertyInfo();
		PropertyInfo updatecampo = new PropertyInfo();
		PropertyInfo updatenuevoRutName = new PropertyInfo();

		updatenomRut.setName("rut");
		updatenomRut.setValue(nomRut);
		updatenomRut.setType(String.class);
		updatecampo.setName("campo");
		updatecampo.setValue(campo);
		updatecampo.setType(int.class);
		updatenuevoRutName.setName("verificador");
		updatenuevoRutName.setValue(nuevoRutName);
		updatenuevoRutName.setType(String.class);

		request.addProperty(updatenomRut);
		request.addProperty(updatecampo);
		request.addProperty(updatenuevoRutName);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}
	public String borrarPersona(String rut){
		coneccionwsPropio();
		METHOD_NAME = "borrar";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo borrarRut = new PropertyInfo();


		borrarRut.setName("rut");
		borrarRut.setValue(rut);
		borrarRut.setType(String.class);


		request.addProperty(borrarRut);


		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}      

	public String findPersona(String buscar){
		coneccionwsPropio();
		METHOD_NAME = "find";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findBuscar = new PropertyInfo();


		findBuscar.setName("buscar");
		findBuscar.setValue(buscar);
		findBuscar.setType(String.class);


		request.addProperty(findBuscar);


		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}      
	public String finAllPersona(String buscar){
		coneccionwsPropio();
		METHOD_NAME = "borrar";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findBuscar = new PropertyInfo();


		findBuscar.setName("buscar");
		findBuscar.setValue(buscar);
		findBuscar.setType(String.class);


		request.addProperty(findBuscar);


		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	} 
}
